package com.cloud.diffdemo.callback

import android.text.TextUtils
import androidx.recyclerview.widget.DiffUtil
import com.cloud.diffdemo.model.UserBean


/**
 * @auth: njb
 * @date: 2024/11/14 23:01
 * @desc: 数据回调
 */
 class EasyDiffCallBack(private val oldUserList: List<UserBean>, private val newUserList: List<UserBean>) : DiffUtil.Callback() {

    override fun getOldListSize(): Int {
        return oldUserList.size ?: 0
    }

    override fun getNewListSize(): Int {
        return newUserList.size ?: 0
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
       val oldUserBean = oldUserList[oldItemPosition]
        val newUserBean = oldUserList[newItemPosition]
        if (oldUserBean.toString().isNotEmpty() && newUserBean.toString().isNotEmpty()){
            val oldId = oldUserList[oldItemPosition].id
            val newId = newUserList[newItemPosition].id
            if (oldId == newId){
                return true
            }
        }
        return false
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        val oldName = oldUserList[oldItemPosition].name
        val newName = newUserList[newItemPosition].name
        if (TextUtils.isEmpty(oldName) || TextUtils.isEmpty(newName)){
            return false
        }
        if (oldName == newName){
            return true
        }
        return false
    }

    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        return super.getChangePayload(oldItemPosition, newItemPosition)
    }

}